import { APPTOKENKEY, IFRAMETOKENKEY, MICROTOKENKEY } from '@/common';
import { isEmpty } from 'lodash-es';

export const getCurrToken: () => string | null = () => {
  let tempTokenKey = '';
  if (window.__POWERED_BY_QIANKUN__) {
    // 微应用
    tempTokenKey = MICROTOKENKEY;
  } else if (window.__POWERED_BY_IFRAME__) {
    //iframe
    tempTokenKey = IFRAMETOKENKEY;
  } else {
    tempTokenKey = APPTOKENKEY;
  }
  return localStorage.getItem(tempTokenKey);
};

// 接口请求数据处理, null和undefined去掉
export const requestParamsFormat = (formData: any) => {
  if (isEmpty(formData)) {
    return formData;
  }
  if (typeof formData === 'object' && !Array.isArray(formData)) {
    Object.keys(formData).forEach((item) => {
      if (isEmpty(formData[item]) && typeof formData[item] !== 'number' && typeof formData[item] !== 'boolean') {
        // isEmpty(number) === true
        delete formData[item];
      } else if (typeof formData[item] === 'object') {
        requestParamsFormat(formData[item]);
      }
    });
  } else if (Array.isArray(formData)) {
    formData.forEach((el) => {
      if (!isEmpty(el) && typeof el === 'object') {
        requestParamsFormat(el);
      }
    });
  }
  return formData;
};

// 上下午 type 1: hh:mm
export const getUpDownDay = (time: string, type: number) => {
  let t = '';
  if (type == 1) t = time.slice(0, 2);
  else t = time;
  if (+t < 13) return '上午';
  else return '下午';
};

export const priceSwitch = (x: number | string) => {
  // x = String(x)
  // //强制保留两位小数
  // let f = parseFloat(x)
  // if (isNaN(f)) return false
  // f = Math.round(Number(x) * 100) / 100
  // let s = f.toString()
  // let rs = s.indexOf(".")
  // if (rs < 0) {
  //   rs = s.length
  //   s += "."
  // }
  // while (s.length < rs + 1 + 2) {
  //   s += "0"
  // }
  //每三位用一个逗号隔开
  const s = String(x);
  let leftNum = s.split('.')[0];
  const rightNum = s.split('.')[1] ? '.' + s.split('.')[1] : '';
  let result;
  //定义数组记录截取后的价格
  const resultArray = [];
  if (leftNum.length > 3) {
    let i = true;
    while (i) {
      resultArray.push(leftNum.slice(-3));
      leftNum = leftNum.slice(0, leftNum.length - 3);
      if (leftNum.length < 4) {
        i = false;
      }
    }
    //由于从后向前截取，所以从最后一个开始遍历并存到一个新的数组，顺序调换
    const sortArray = [];
    for (let i = resultArray.length - 1; i >= 0; i--) {
      sortArray.push(resultArray[i]);
    }
    result = leftNum + ',' + sortArray.join(',') + rightNum;
  } else {
    result = s;
  }
  return result;
};

export const priceSwitchUnit = (value: number | string) => {
  value = Number(value);
  //小数保留两位，整数不显示小数 Math.round(value*100)/100
  if (value / 100000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 100000000) * 100) / 100);
    return m + ' 亿';
  } else if (value / 10000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 10000000) * 100) / 100);
    return m + ' 千万';
  } else if (value / 1000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 1000000) * 100) / 100);
    return m + ' 百万';
  } else if (value / 100000 >= 1) {
    const m = priceSwitch(Math.floor((value / 100000) * 100) / 100);
    return m + ' 十万';
  } else if (value / 10000 >= 1) {
    const m = priceSwitch(Math.floor((value / 10000) * 100) / 100);
    return m + ' 万';
  } else {
    const m = priceSwitch(Math.floor(value * 100) / 100);
    return m;
  }
};

type TransformTreeParams = {
  list: any[];
  parentId: string | number; // 父级id
  parentKey: string; // 父级id键名
  key?: string | 'id'; // 子级id键名
  children?: string; // 子级列表名
  handleItem?: (item: any) => any;
};

export const getTreeByList = (params: TransformTreeParams) => {
  const { list, parentId, parentKey, key = 'id', handleItem, children = 'children' } = params;
  const tree = list.filter((item) => item[parentKey] === parentId);
  const childrenList = list.filter((item) => item[parentKey] !== parentId);

  return tree.map((item) => {
    item[children] = getTreeByList({
      list: childrenList,
      parentId: item[key],
      key,

      parentKey,
      children,
      handleItem,
    });
    return handleItem ? handleItem(item) : item;
  });
};

export function maskIdCard(idCard: string) {
  // 正则表达式匹配前6位和后4位
  const reg = /^(.{6})(\d+)(.{4})$/;
  // 使用正则表达式进行替换
  const maskedIdCard = idCard.replace(reg, '$1*********$3');
  return maskedIdCard;
}
